/**
 * 
 */
package leetCode;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 * @author zhongfang
 *
 */
public class ExclusiveTimeofFunctions {
    public static int[] exclusiveTime(int n, List<String> logs) {
        int[] res=new int[n];
        int beforeTime=0;
        Stack<Integer> idStack=new Stack<>();
        for (int i = 0; i < logs.size(); i++) {
        	String cur=logs.get(i);
        	String[] strs=cur.split(":");
        	int id=Integer.parseInt(strs[0]);
        	String type=strs[1];
        	int time=Integer.parseInt(strs[2]);
        	int spareTime=time-beforeTime;
        	if(type.equals("start")){
        		if(!idStack.isEmpty())
            		res[idStack.peek()]+=spareTime;
        		idStack.push(id);
        	}else {
        		time++;
        		spareTime++;
        		res[idStack.pop()]+=spareTime;
			}
        	beforeTime=time;
		}
        return res;
    }
	/**
	 * @param args
	 */
	public static void main(String[] args) {
//		2
//		{"0:start:0","0:start:2","0:end:5","1:start:7","1:end:7","0:end:8"}
//		{"0:start:0","1:start:2","1:end:5","0:end:6"}
		String[] logs={"0:start:0","1:start:2","1:end:5","0:end:6"};
		List<String> isList=new ArrayList<>();
		for (String string : logs) {
			isList.add(string);
		}
		int[] r=exclusiveTime(2, isList);
		for (int i : r) {
			System.out.println(i);
		}
	}

}
